package com.ruoyi.app.controller.teacher;

import com.ruoyi.app.domain.AppTeacher;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.app.controller.teacher.vo.AppTeacherCreateReqVo;
import com.ruoyi.app.controller.teacher.vo.AppTeacherRespVo;
import com.ruoyi.app.controller.teacher.vo.AppTeacherUpdateReqVo;

import com.ruoyi.app.service.IAppTeacherService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 老师Controller
 * 
 * @author ruoyi
 * @date 2025-01-11
 */
@RestController
@RequestMapping("/app/teacher")
public class AppTeacherController extends BaseController
{
    @Autowired
    private IAppTeacherService schTeacherService;
    @Autowired
    private ISysUserService userService;

    @GetMapping("/listForSelect")
    public AjaxResult listForSelect( )
    {
        List<AppTeacherRespVo> list = schTeacherService.selectAppTeacherList(new AppTeacher());
        return AjaxResult.success(list);
    }
    /**
     * 查询老师列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppTeacher schTeacher)
    {
        startPage();
        List<AppTeacherRespVo> list = schTeacherService.selectAppTeacherList(schTeacher);
        return getDataTable(list);
    }

    /**
     * 导出老师列表
     */
    @Log(title = "老师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppTeacher schTeacher)
    {
        List<AppTeacherRespVo> list = schTeacherService.selectAppTeacherList(schTeacher);
        ExcelUtil<AppTeacherRespVo> util = new ExcelUtil<AppTeacherRespVo>(AppTeacherRespVo.class);
        util.exportExcel(response, list, "老师数据");
    }

    /**
     * 获取老师详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(schTeacherService.selectAppTeacherById(id));
    }

    /**
     * 新增老师
     */
    @Log(title = "老师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppTeacherCreateReqVo req)
    {
        SysUser user = new SysUser();
        user.setUserName(req.getUserName());
        user.setPhonenumber(req.getPhonenumber());
        user.setEmail(req.getEmail());

        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }

        return toAjax(schTeacherService.insertAppTeacher(req));
    }

    /**
     * 修改老师
     */
    @Log(title = "老师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppTeacherUpdateReqVo req)
    {
        SysUser user = new SysUser();
        user.setUserName(req.getUserName());
        user.setPhonenumber(req.getPhonenumber());
        user.setEmail(req.getEmail());
        user.setUserId(req.getUserId());
        if (!userService.checkUserNameUnique(user)) {
            return error("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) {
            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) {
            return error("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        return toAjax(schTeacherService.updateAppTeacher(req));
    }

    /**
     * 删除老师
     */
    @Log(title = "老师", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schTeacherService.deleteAppTeacherByIds(ids));
    }
}
